﻿using CommonTools;
using CommonTools.Controls;
using Newtonsoft.Json;
using shop.Bll;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shop.cost
{
    /// <summary> 分配到物流公司 </summary>
    public partial class PostCalcStrategyEditor2 : Form
    {
        PostCalcStrategyBll _bll = new PostCalcStrategyBll();
        private List<LogiCalc> _logis;


        public PostCalcStrategyEditor2()
        {
            InitializeComponent();

        }

        private void btnOk_Click(object sender, EventArgs e)
        {

           
        }

        private  void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

  

        private void PostCalcStrategyEditor_Load(object sender, EventArgs e)
        {
          
        }



        private async void PostCalcStrategyEditor_Shown(object sender, EventArgs e)
        {
            var logiNames= await _bll.QueryAllLogis(null);
            _logis= await _bll.QueryLogiCalcs();

            _logis.AddRange(
                logiNames
                .Where(a => _logis.Any(c => c.Logi == a) == false)
                .Select(a => new LogiCalc { CalcMethod = string.Empty, Logi = a })
                .ToArray()
                );

            this.grid2.AddItems(_logis, a => a.Logi, a => a.CalcMethod);
        }

        private async void grid2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            if(e.ColumnIndex == cl_CalcMethod.Index)
            {
                await _bll.SetLogiCalc(grid2[cl_logi.Index, e.RowIndex].Value.IsNull(),
                    grid2[cl_CalcMethod.Index, e.RowIndex].Value.IsNull());

                if (grid2.Rows[e.RowIndex].Tag is LogiCalc calc)
                {
                    calc.CalcMethod = grid2[cl_CalcMethod.Index, e.RowIndex].Value.IsNull();
                }

                MessageTip.ShowOk(this, "保存成功");
            }
        }

        private void box_findLogi_RequestValidate(GuanTextBox arg1, FastValueArgs arg2)
        {
            //if (this.grid2.Rows.Count == 0 || _logis == null)
            //{
            //    return;
            //}

            if (string.IsNullOrEmpty(arg1.Text?.Trim()))
            {
                grid2.Rows.Clear();
                this.grid2.AddItems(_logis, a=> a.Logi, a=> a.CalcMethod);

                return;
            }

            var temp= _logis.Where(a=> a.Logi.Contains(arg1.Text))
                .ToList();

            grid2.Rows.Clear();
            this.grid2.AddItems(temp, a => a.Logi, a => a.CalcMethod);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //if (this.grid2.Rows.Count == 0 || _logis == null)
            //{
            //    return;
            //}

            grid2.Rows.Clear();

            if (checkBox1.Checked) 
            {
                var temp = _logis.Where(a => !string.IsNullOrEmpty(a.CalcMethod))
                    .ToList();

                this.grid2.AddItems(temp, a => a.Logi, a => a.CalcMethod);
            }
            else
            {
                this.grid2.AddItems(_logis, a => a.Logi, a => a.CalcMethod);
            }

        }
    }
}
